//
//  InputPwdView.m
//  IHall
//
//  Created by Soyu on 12-5-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "InputPwdView.h"

@implementation InputPwdView
@synthesize txtPwd;
@synthesize backgroundImage;
@synthesize btnOK;
@synthesize btnCancel;
@synthesize JKdelegate;

- (id)init
{
    self = [super init];

    if (self) {
        // Initialization code
        self.backgroundImage = [UIImage imageNamed:@"PasswordAlert.png"];
        self.txtPwd = [self createTextField :@"请输入密码"
                            withFrame       :CGRectMake(23, 73, 522, 54)];
        self.txtPwd.borderStyle = UITextBorderStyleNone;
        self.txtPwd.font = [UIFont systemFontOfSize:26];

        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.txtPwd];
    }

    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self) {}

    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    for (UIView *v in [self subviews]) {
        if ([v class] == [UIImageView class]) {
            [v setHidden:YES];
        }

        if ([v isKindOfClass:[UIButton class]] ||
            [v isKindOfClass:NSClassFromString(@"UIThreePartButton")]) {
            [v setHidden:YES];
        }
    }

    self.btnOK = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnOK.tag = 1;
    self.btnOK.frame = CGRectMake(99, 160, 154, 68);
    [btnOK addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

    self.btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnCancel.tag = 0;
    self.btnCancel.frame = CGRectMake(335, 160, 156, 66);
    [btnCancel addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.btnOK];
    [self addSubview:self.btnCancel];
}

- (void)show
{
    [super show];
    CGSize imageSize = self.backgroundImage.size;
    self.bounds = CGRectMake(0, 0, imageSize.width, imageSize.height);
    [self.txtPwd becomeFirstResponder];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    CGSize imageSize = self.backgroundImage.size;

    [self.backgroundImage drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
}

- (UITextField *)createTextField:(NSString *)placeholder withFrame:(CGRect)frame
{
    UITextField *field = [[UITextField alloc] initWithFrame:frame];

    field.placeholder = placeholder;
    field.secureTextEntry = YES;
    field.backgroundColor = [UIColor clearColor];
    field.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    return field;
}


- (void)buttonClicked:(id)sender
{
    UIButton *btn = (UIButton *)sender;

    if (JKdelegate) {
        if ([JKdelegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]) {
            [JKdelegate alertView:self clickedButtonAtIndex:btn.tag];
        }
    }

    [self dismissWithClickedButtonIndex:0 animated:YES];
}

@end